package org.zjx.core.impl;

import com.sun.jna.platform.win32.WinDef;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.zjx.command.ICommand;
import org.zjx.command.StrategyCommand;
import org.zjx.core.AbstractWindow;
import org.zjx.state.StateManager;
import org.zjx.util.HwndUtil;

import java.awt.*;
import java.util.concurrent.CompletableFuture;

@EqualsAndHashCode(callSuper = true)
@Data
@Slf4j
public class GameWindow extends AbstractWindow {
    private final StateManager stateManager;

    public GameWindow(WinDef.HWND hwnd,StateManager stateManager) {
        super(hwnd);
        this.stateManager = stateManager;
        submitCommand(new StrategyCommand(this,stateManager));
    }

    @Override
    public Rectangle getTrueBounds() {
        return new Rectangle(8,31,800,600);
    }

    @Override
    public String getId() {
        return HwndUtil.getWindowTitle(hwnd);
    }


    @Override
    public void executeCommand(ICommand command) {
        super.executeCommand(command);

        // 命令执行成功后检查队列状态
        checkAndSubmitStrategyCommand();
    }

    /**
     * 检查并提交策略命令
     */
    public void checkAndSubmitStrategyCommand() {
        try {
            // 检查命令队列是否为空
            if (getCommandQueue().isEmpty()) {
                // 队列为空，提交策略命令
                log.info("[GameWindow] 检测到命令队列为空，准备提交策略命令: {}", getId());

                StrategyCommand strategyCommand = new StrategyCommand(this, stateManager);
                submitCommand(strategyCommand);
                log.debug("[GameWindow] 策略命令已提交: {}", getId());
            }
        } catch (Exception e) {
            log.error("[GameWindow] 检查并提交策略命令失败: {}", getId(), e);
        }
    }
}